<style lang="less">
    @import "pay.less";
</style>
<template>
    <div class="search">
        <Row>
            <Form ref="listQuery" :model="listQuery" inline :label-width="100" >
                <Form-item label="申请编号" prop="applyNo">
                    <Input type="text" v-model="listQuery.applyNo" clearable placeholder="请输入申请编号"
                           />
                </Form-item>
                <Form-item label="合同编号" prop="contractNo">
                    <Input type="text" v-model="listQuery.contractNo" clearable placeholder="请输入合同编号"
                           />
                </Form-item>
                <Form-item label="客户姓名" prop="custName">
                    <Input type="text" v-model="listQuery.custName" clearable placeholder="请输入客户名称"
                           />
                </Form-item>
                <Form-item label="产品名称" prop="productName">
                    <Input type="text" v-model="listQuery.productName" clearable placeholder="请输入产品名称"
                           />
                </Form-item>
                <Form-item label="渠道名称" prop="channelName">
                    <Input type="text" v-model="listQuery.channelName" clearable placeholder="请输入渠道名称"
                           />
                </Form-item>
                <Form-item label="业务类型" prop="businessType">
                    <Select v-model="listQuery.businessType"  clearable placeholder="请选择业务类型">
                        <Option v-for="item in dataDic.businessType" :value="item.value" :label="item.title" :key="item.value">{{ item.title }}</Option>
                    </Select>
                </Form-item>
                <Form-item label="激活日期" prop="startDate">
                    <DatePicker v-model="selectDate" type="daterange" format="yyyy-MM-dd" clearable
                                @on-change="selectDateRange" placeholder="选择激活日期">
                    </DatePicker>
                </Form-item>
                <Form-item label="付款银行" prop="paymentBankCode">
                    <Select v-model="listQuery.paymentBankCode"  clearable placeholder="请选择付款银行">
                        <Option v-for="item in dataDic.payBank" :value="item.value" :label="item.title" :key="item.value">{{ item.title }}</Option>
                    </Select>
                </Form-item>
                <Form-item label="自动付款状态" prop="isPayment">
                    <Select v-model="listQuery.isPayment"  clearable placeholder="请选择自动付款状态">
                        <Option value="no" label="暂不自动付款" >暂不自动付款</Option>
                        <Option value="yes" label="可自动付款" >可自动付款</Option>
                    </Select>
                </Form-item>
                <Form-item label="付款状态" prop="status">
                    <Select v-model="listQuery.status"  clearable placeholder="请选择付款状态">
                        <Option value="waitPayment" label="待付款" >待付款</Option>
                        <Option value="failPayment" label="付款失败" >付款失败</Option>
                    </Select>
                </Form-item>
                <Form-item label="账号修改状态" prop="accountChangeStatus">
                    <Select v-model="listQuery.accountChangeStatus"  clearable placeholder="请选择付款银行">
                        <Option v-for="item in dataDic.accountChangeStatus" :value="item.value" :label="item.title" :key="item.value">{{ item.title }}</Option>
                    </Select>
                </Form-item>
            </Form>
        </Row>
        <Row>
            <Col align="middle">
                <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                <Button @click="handleReset" type="primary" icon="ios-trash">重置</Button>
            </Col>
        </Row>
        <br />
        <Row class="operation">
            <Button @click="exportData" type="primary" icon="md-list-box">导出</Button>
            <Button @click="sendPayOrder" type="primary" >发送付款</Button>
            <Button @click="sendMsgToAdmin" type="primary" >发送收款账号修改短信</Button>
            <Button @click="updateAccount" type="primary" >更新收款账号</Button>
            <Button @click="stopPay" type="primary" >暂停自动付款</Button>
            <Button @click="cancelStopPay" type="primary" >取消暂停自动付款</Button>
        </Row>
        <Row>
            <Alert show-icon>
                已选择 <span class="select-count">{{selectCount}}</span> 项
                <a class="select-clear" @click="clearSelectAll">清空</a>
            </Alert>
        </Row>
        <Row>
            <Table :loading="listLoading" border :columns="columns" :data="list"sortable="custom"
                   @on-selection-change="showSelect" ref="table">
                <template slot-scope="{ row, index }" slot="receiptBankCode">
                    <span>{{setDictTitleByType("bankCode",row.receiptBankCode)}}</span>
                </template>
                <template slot-scope="{ row, index }" slot="accountChangeStatus">
                    <span>{{setDictTitleByType("accountChangeStatus",row.accountChangeStatus)}}</span>
                </template>
                <template slot-scope="{ row, index }" slot="isPayment">
                    <span>{{takeSpecialTranslation(row.isPayment)}}</span>
                </template>
                <template slot-scope="{ row, index }" slot="paymentStatus">
                    <span>{{takeSpecialTranslation(row.paymentStatus)}}</span>
                </template>
                <template slot-scope="{ row, index }" slot="paymentAmt">
                    <span>{{row.paymentAmt==""?"":row.paymentAmt.toFixed(2)}}</span>
                </template>
            </Table>
        </Row>
        <br>
        <Row type="flex" justify="end" class="page">
            <Page :current="listQuery.pageNum" :total="total" :page-size="listQuery.pageSize"
                  @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]"
                  size="small" show-total show-elevator show-sizer></Page>
        </Row>
    </div>
</template>

<script>
    import {
        getByTypes
    } from "_p/basic/api/admin/datadic";
    import {
        queryPayRecordList,
        sendPayOrderForIds,
        sendMsgToAdmin,
        updateReceiptAccount,
        cancelStopPayForIds,
        exportPayList,
        stopPayForIds
    } from "@/projects/basic/api/contract/pay/pay";
    export default {
        name: "unpaid",
        data() {
            return {
                columns: [
                    {type: "selection",minWidth: 60,maxWidth:60,align: "center",fixed: "left"},
                    {title: "申请编号",key: "applyNo",minWidth: 160,align: "center"},
                    {title: "合同号",key: "contractNo",minWidth: 180,align: "center"},
                    {title: "自动付款状态",key: "isPayment",minWidth: 150,align: "center",slot:"isPayment"},
                    {title: "付款状态",key: "paymentStatus",minWidth: 150,align: "center",slot:"paymentStatus"},
                    {title: "客户姓名",key: "custName",minWidth: 150,align: "center"},
                    {title: "收款账号",key: "receiptAccount",minWidth: 150,align: "center"},
                    {title: "收款账户名称",key: "receiptAccountName",minWidth: 200,align: "center"},
                    {title: "收款银行",key: "receiptBankCode",minWidth: 150,align: "center",slot:"receiptBankCode"},
                    {title: "金额",key: "paymentAmt",slot: "paymentAmt",minWidth: 150,align: "center"},
                    {title: "金融产品",key: "productName",minWidth: 150,align: "center"},
                    {title: "经销商",key: "channelName",minWidth: 200,align: "center"},
                    {title: "激活日期",key: "startDate",minWidth: 150,align: "center"},
                    {title: "失败原因",key: "lastFailReason",minWidth: 150,align: "center"},
                    {title: "收款账号修改状态",key: "accountChangeStatus",minWidth: 180,align: "center",slot:"accountChangeStatus"},
                ],
                dicKeys: [
                    "payBank","businessType","bankCode","accountChangeStatus"
                ],
                dataDic:{},
                selectDate: null,
                list: [],
                dataForExport:[],
                total: 0,
                listLoading: false,
                listQuery: {
                    applyNo:"",contractNo:"",custName:"",productName:"",channelName:"",businessType:"",
                    paymentBankCode:"",startDate:"",endDate:"",paymentStatus:"waitPayment,failPayment",
                    accountChangeStatus:"",
                    pageNum: 1,
                    pageSize: 10
                },
                selectCount: 0,
            }
        },
        methods:{
            exportData (type) {
                this.dataForExport = [];
                exportPayList(this.listQuery).then(res => {
                    if (res.code === "0000") {
                        this.dataForExport = res.data.records;
                        this.dataForExport.forEach(line =>{
                            line.isPayment = this.takeSpecialTranslation(line.isPayment);
                            line.receiptAccount = "\t" + line.receiptAccount;
                            line.paymentStatus = this.takeSpecialTranslation(line.paymentStatus);
                            line.receiptBankCode = this.setDictTitleByType("bankCode",line.receiptBankCode);
                            line.accountChangeStatus = this.setDictTitleByType("accountChangeStatus",line.accountChangeStatus);
                        });
                        this.$refs.table.exportCsv({
                            filename: '待付款清单',
                            columns: this.columns.filter((col, index) => index > 0),
                            data: this.dataForExport
                        });
                    }else{
                        this.$Message.error("导出失败");
                    }
                });
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            selectDateRange(v) {
                if (v) {
                    this.listQuery.startDate = v[0];
                    this.listQuery.endDate = v[1];
                }
            },
            initDataDic(){
                let self = this;
                getByTypes(this.dicKeys).then(res => {
                    if (res.code === "0000" && res.data) {
                        self.dataDic = res.data;
                    }
                });
            },
            takeSpecialTranslation(v){
                let dic = ""
                switch(v) {
                    case "no":
                        dic = "暂不自动付款";
                        break;
                    case "yes":
                        dic = "可自动付款";
                        break;
                    case "waitPayment":
                        dic = "待付款";
                        break;
                    case "failPayment":
                        dic = "付款失败";
                        break;
                    default:
                }
                return dic;
            },
            getDic(v){
                let dic = []
                switch(v) {
                    case "bankCode":
                        dic = this.dataDic.bankCode;
                        break;
                    case "accountChangeStatus":
                        dic = this.dataDic.accountChangeStatus;
                        break;
                    default:
                }
                return dic;
            },
            setDictTitleByType(v1,v2){
                let dic = {}
                if(!this.getDic(v1)) return '';
                this.getDic(v1).forEach(column => {
                    if(column.value === v2){
                        dic = column;
                    }
                });
                return dic.title;
            },
            handleSearch() {
                this.listQuery.pageNum = 1;
                this.listQuery.pageSize = 10;
                this.getPayRecordList();
            },
            handleReset() {
                this.$refs.listQuery.resetFields();
                this.listQuery.pageNum = 1;
                this.listQuery.pageSize = 10;
                this.selectDate = null;
                this.listQuery.startDate = "";
                this.listQuery.endDate = "";
                // 重新加载数据
                this.getPayRecordList();
            },
            changePage(v) {
                this.listQuery.pageNum = v;
                this.getPayRecordList();
            },
            changePageSize(v) {
                this.listQuery.pageSize = v;
                this.getPayRecordList();
            },
            getPayRecordList(){
                this.listLoading = true;
                queryPayRecordList(this.listQuery).then(res => {
                    this.listLoading = false;
                    if (res.code === "0000") {
                        this.list = res.data.records;
                        this.total = res.data.total;
                    }
                });
                this.selectCount=0;
            },
            sendPayOrder(){
                if (this.selectCount <= 0 || this.selectCount == undefined) {
                    this.$Message.warning("请勾选数据");
                    return;
                }
                let ldStr = "";
                let flag = true;
                this.selectList.forEach(function (e) {
                    if(e.isPayment == "no"){
                        ldStr += e.id + ",";
                    }else{
                        flag = false;
                    }
                });
                if (!flag){
                    this.$Message.warning("只有暂不自动付款的数据可以发送付款");
                    return;
                }
                let params = {
                    ids :ldStr
                }
                sendPayOrderForIds(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.data);
                        this.getPayRecordList();
                    }else{
                        this.$Message.error(res.data);
                        this.getPayRecordList();
                    }
                }).catch(e => {
                    this.getPayRecordList();
                });
            },
            stopPay(){
                if (this.selectCount <= 0 || this.selectCount == undefined) {
                    this.$Message.warning("请勾选数据");
                    return;
                }
                let ldStr = "";
                this.selectList.forEach(function (e) {
                    ldStr += e.id + ",";
                });
                let params = {
                    ids :ldStr
                }
                stopPayForIds(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.data);
                        this.getPayRecordList();
                    }else{
                        this.$Message.error(res.data);
                    }
                }).catch(e => {
                    this.$Message.success("操作失败");
                });
            },
            cancelStopPay(){
                if (this.selectCount <= 0 || this.selectCount == undefined) {
                    this.$Message.warning("请勾选数据");
                    return;
                }
                let ldStr = "";
                this.selectList.forEach(function (e) {
                    ldStr += e.id + ",";
                });
                let params = {
                    ids :ldStr
                }
                cancelStopPayForIds(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.data);
                        this.getPayRecordList();
                    }
                });
            },
            sendMsgToAdmin(){
                if (this.selectCount <= 0 || this.selectCount == undefined) {
                    this.$Message.warning("请勾选数据");
                    return;
                }
                let ldStr = "";
                this.selectList.forEach(function (e) {
                    ldStr += e.id + ",";
                });
                let params = {
                    ids :ldStr
                }
                sendMsgToAdmin(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.data);
                        this.getPayRecordList();
                    }
                }).catch(e =>{
                    this.$Message.error(e.msg);
                    this.getPayRecordList();
                });

            },
            updateAccount(){
                if (this.selectCount <= 0 || this.selectCount == undefined) {
                    this.$Message.warning("请勾选数据");
                    return;
                }
                let ldStr = "";
                this.selectList.forEach(function (e) {
                    ldStr += e.id + ",";
                });
                let params = {
                    ids :ldStr
                }
                updateReceiptAccount(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.data);
                        this.getPayRecordList();
                    }
                }).catch(e =>{
                    this.$Message.error(e.msg);
                    this.getPayRecordList();
                });

            },
            init(){
                this.initDataDic();
                this.listQuery.pageNum = 1;
                this.listQuery.pageSize = 10;
                this.getPayRecordList();
            }
        },
        mounted() {
            this.init();
        }
    }
</script>
